using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Collections;
using System.IO;
using XProject.DAO;
using XProject.Entity;
namespace XProject.GUI
{
    
    public partial class Login : Form
    {
        AccountDAO daoAccount = new AccountDAO();
        Account account = new Account();
        bool remembered = false;
        public Account Account
        {
            get { return account; }
        }
        public TextBox UsernameBox
        {
            get { return txtUsername; }
        }
        public TextBox PasswordBox
        {
            get { return txtPassword; }
        }
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            if (File.Exists(Manager.startup_path + "\\" + "config_data.dat"))
            {
                try
                {                  
                    StreamReader r = new StreamReader(Manager.startup_path+"\\"+"config_data.dat");
                    bool remember = Convert.ToBoolean(r.ReadLine());
                    remembered = remember;
                    if (remember)
                    {
                        txtUsername.Text = r.ReadLine();
                        txtPassword.Text = r.ReadLine();
                    }
                    r.Close();
                    chkRemember.Checked = remember;
                }
                catch (IOException ex) { MessageBox.Show(ex.Message,"Error"); }
                catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); }
            }
        }

        private void groupBox1_DragDrop(object sender, DragEventArgs e)
        {
            this.Left = e.X;
            this.Top = e.Y;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            
            account.Username=txtUsername.Text;
            account.Password=Util.Encryptor.MD5Encrypt(txtPassword.Text);
            if (daoAccount.GetAccount(account))
            {
                this.DialogResult = DialogResult.Yes;
                if (chkRemember.Checked)
                {
                    StreamWriter w = new StreamWriter(Manager.startup_path + "\\" + "config_data.dat");
                    w.WriteLine(true);
                    w.WriteLine(txtUsername.Text);
                    w.WriteLine(txtPassword.Text);
                    w.Close();
                }
                else
                {
                    if (remembered)
                    {
                        StreamWriter w = new StreamWriter(Manager.startup_path + "\\" + "config_data.dat");
                        w.WriteLine(false);
                        w.WriteLine();
                        w.WriteLine();
                        w.Close();
                    }
                }
            }
            else this.DialogResult = DialogResult.No;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}